﻿using System;
using System.Collections.Generic;
using System.Linq;
using DbUp.ScriptProviders;
using DbUp.Specification.Contexts;
using NSubstitute;
using NUnit.Framework;

namespace DbUp.Specification.Concerning
{
    public class UpgradingAnOutOfDateDatabaseWithDuplicateVersionScripts : EmptyDatabase
    {
        [SetUp]
        public override void BeforeEach()
        {
            base.BeforeEach();
            Guid version = Guid.NewGuid();
            AllScripts = new List<SqlScript> {
                                                 new SqlScript("0001.southwind.sql", "--LITTLE BOBBY DROP TABLES WAS HERE.".ToSampleScript(version)),
                                                 new SqlScript("0002.southwind.sql", "CREATE TABLE USERS --AGAIN".ToSampleScript(version))
                                             };

            var appliedScript = AllScripts.First();
            ScriptProvider.GetScripts().Returns(AllScripts);
            VersionTracker.GetExecutedScripts().Returns(new List<SqlScriptAlreadyApplied>
                                                               {
                                                                   new SqlScriptAlreadyApplied(
                                                                       appliedScript.VersionKey,
                                                                       "0001.southwind.sql", "",
                                                                       DateTime.Now, "")
                                                               });
        }



        [Test]
        public void ShouldFailIfScriptsReportDuplicateGuidVersions()
        {
            var result = DbUpgrader.PerformUpgrade();
            Assert.IsNotNull(result.Error);
            Assert.IsInstanceOf(typeof(DuplicateVersionKeyFoundException), result.Error);
        }
    }
}